from LeetTool import ListNode
from LeetTool import build_ListNode


class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        ans = node = ListNode(0)
        carry = 0
        while l1 or l2 or carry:
            val = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry
            carry, now = divmod(val, 10)
            node.next = ListNode(now)
            node = node.next
            l1 = l1.next if l1 else None
            l2 = l2.next if l2 else None
        return ans.next


if __name__ == "__main__":
    # [7,0,8]
    print(Solution().addTwoNumbers(build_ListNode([2, 4, 3]), build_ListNode([5, 6, 4])))

    # [0]
    print(Solution().addTwoNumbers(build_ListNode([0]), build_ListNode([0])))

    # [8,9,9,9,0,0,0,1]
    print(Solution().addTwoNumbers(build_ListNode([9, 9, 9, 9, 9, 9, 9]), build_ListNode([9, 9, 9, 9])))
